/*
 * Created on Wed Jun 06 2018 by Ronnie Ren (zhaosong.ren) from Accenture
 *
 * Copyright (c) 2018 DingXing information & technology limited company.
 */

import { Injectable } from "@angular/core";
// import { UserService } from "../models/transform.service";
import { SqliteService } from "../../../services/sqliteDB/sqlite.service";
// import { BillService } from "./bill.service";

import {JsonMappingUtil} from "../../json-mapping-util";
import { Observable } from "rxjs";
import { CurdService } from "../curd.service";
import { SyncUtilService } from "../../../services/offline/sync.util.service";
import { WorkTicketDto } from "../../dto/inspection/work-ticket.dto";
import { WorkTicket } from "../../transform/inspection/work-ticket";



@Injectable()
export class WorkTicketService {

    constructor(
        private sqliteService: SqliteService,
        private curdService: CurdService,
    ) {

    }

    getWorkTicket(plannedId: string) {
        // return  this.curdService.observableFetchObjects(WorkTicketDto , {taskId: plannedId})
        // .map((objs: Array<{}>) => {
        //     if (objs && objs.length > 0) {
        //         return objs.map((obj) => {
        //             let dto = new WorkTicketDto();
        //             return JsonMappingUtil.MappingAllProp(obj, dto);
        //         });
        //     }
        //     return null; 
        // })
        // .map((dtos) => dtos.map((dto) => {
        //     if (dto) {
        //         let model = new  WorkTicket({});
        //         return JsonMappingUtil.MappingAllProp(dto, model);
        //     }
        //     return null;
            
        // }));

        return this.curdService.observableFetchObjects(WorkTicketDto , {taskId: plannedId})
        .map((objs: Array<{}>) => {
            if (objs && objs.length > 0) {
                let dto = new WorkTicketDto();
                return JsonMappingUtil.MappingAllProp(objs[0], dto);
            }
            return null;
        })
        .map((dto) => {
            if (dto) {
                let model = new WorkTicket({});
                return JsonMappingUtil.MappingAllProp(dto, model);
            }
            return null;

        });
    }
}
